home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / gedit-2 / plugins / snippets / Importer.py < prev    next >
Encoding:
Python Source  |  2009-04-14  |  3.7 KB  |  101 lines

  1. import os
  2. import tempfile
  3. import sys
  4. import shutil
  5.  
  6. from snippets.Library import *
  7.  
  8. class Importer:
  9.         def __init__(self, filename):
  10.                 self.filename = filename
  11.  
  12.         def import_destination(self, filename):
  13.                 userdir = Library().userdir
  14.                 
  15.                 filename = os.path.basename(filename)
  16.                 (root, ext) = os.path.splitext(filename)
  17.  
  18.                 filename = os.path.join(userdir, root + ext)
  19.                 i = 1
  20.                 
  21.                 while os.path.exists(filename):
  22.                         filename = os.path.join(userdir, root + '_' + str(i) + ext)
  23.                         i += 1
  24.                 
  25.                 return filename
  26.                 
  27.         def import_file(self, filename):
  28.                 if not os.path.exists(filename):
  29.                         return _('File `%s` does not exist') % filename
  30.                 
  31.                 if not os.path.isfile(filename):
  32.                         return _('File `%s` is not a valid snippets file') % filename
  33.                 
  34.                 # Find destination for file to copy to
  35.                 dest = self.import_destination(filename)
  36.  
  37.                 # Copy file
  38.                 shutil.copy(filename, dest)
  39.                 
  40.                 # Add library
  41.                 if not Library().add_user_library(dest):
  42.                         return _('Imported file `%s` is not a valid snippets file') % os.path.basename(dest)
  43.         
  44.         def import_xml(self):
  45.                 return self.import_file(self.filename)
  46.  
  47.         def import_archive(self, cmd):
  48.                 dirname = tempfile.mkdtemp()
  49.                 status = os.system('cd %s; %s "%s"' % (dirname, cmd, self.filename))
  50.                 
  51.                 if status != 0:
  52.                         return _('The archive `%s` could not be extracted' % self.filename)
  53.                 
  54.                 errors = []
  55.  
  56.                 # Now import all the files from the archive
  57.                 for f in os.listdir(dirname):
  58.                         f = os.path.join(dirname, f)
  59.  
  60.                         if os.path.isfile(f):
  61.                                 if self.import_file(f):
  62.                                         errors.append(os.path.basename(f))
  63.                         else:
  64.                                 sys.stderr.write('Skipping %s, not a valid snippets file' % os.path.basename(f))
  65.                 
  66.                 # Remove the temporary directory
  67.                 shutil.rmtree(dirname)
  68.  
  69.                 if len(errors) > 0:
  70.                         return _('The following files could not be imported: %s') % ', '.join(errors)
  71.                         
  72.         def import_targz(self):
  73.                 self.import_archive('tar -x --gzip -f')
  74.         
  75.         def import_tarbz2(self):
  76.                 self.import_archive('tar -x --bzip2 -f')
  77.  
  78.         def import_tar(self):
  79.                 self.import_archive('tar -xf')
  80.         
  81.         def run(self):
  82.                 if not os.path.exists(self.filename):
  83.                         return _('File `%s` does not exist') % self.filename
  84.                 
  85.                 if not os.path.isfile(self.filename):
  86.                         return _('File `%s` is not a valid snippets archive') % self.filename
  87.                 
  88.                 (root, ext) = os.path.splitext(self.filename)
  89.                 
  90.                 actions = {'.tar.gz': self.import_targz,
  91.                            '.tar.bz2': self.import_tarbz2,
  92.                            '.xml': self.import_xml,
  93.                            '.tar': self.import_tar}
  94.  
  95.                 for k, v in actions.items():
  96.                         if self.filename.endswith(k):
  97.                                 return v()
  98.                         
  99.                 return _('File `%s` is not a valid snippets archive') % self.filename
  100. # ex:ts=8:et:
  101.